﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UI;
using UnityEngine.UI;
using Base.Extensions;
using HighlightingSystem;
using Base.Custom;
using Base.Data;
public class AddMeshCollider : BasePanel
{
    public Transform[] tran;
    private Transform[] trans;
    private int index;
    // 滑动条  
    public Slider processBar;
    //显示框
    public Text provessText;
    //当前加载
    public Text textlog;
    public float nowprocess;
    //实例化的面板归为它的子物体
    private Transform canvasTransform;
    public Transform mianCamer;
    // Use this for initialization
    void Start()
    {
        canvasTransform = GameObject.Find("Main()/1").transform;
        mianCamer = GameObject.Find("Main Camera").transform;
        trans = canvasTransform.GetComponentsInChildren<Transform>();
        Debug.LogError("总数："+trans.Length);
        processBar.maxValue = trans.Length;
        //StartCoroutine(loadScene());
        for (int i = 0; i < trans.Length; i++)
        {
            if (!trans[i].gameObject.activeSelf)
                continue;
            MeshFilter mesh = trans[i].GetComponent<MeshFilter>();
            if (mesh != null)
            {
                //mesh.gameObject.AddComponent<SingleObject>().SetSingleObject(trans[i].gameObject,);
            }
            processBar.value = i;
            provessText.text = ((int)((float)i / tran.Length)*100)+"%";
            textlog.text = trans[i].name;
        }


    }
    public override void OnEnter()
    {
        gameObject.SetActive(true);
    }
    public override void OnExit()
    {
        for (int i = 0; i < canvasTransform.childCount; i++)
        {
            if (canvasTransform.GetChild(i).name!= "MinimapCamera" && canvasTransform.GetChild(i).name!= "室外场地"&& canvasTransform.GetChild(i).name != "整体外部框架合图")
            {
                canvasTransform.GetChild(i).gameObject.SetActive(false);
            }
        }
        mianCamer.SetEnable<FreeCamera>(true);
        mianCamer.SetEnable<FreeCamera>(true);
        mianCamer.SetEnable<HighlightingRenderer>(true);
        gameObject.SetActive(false);
        Debug.LogError("界面退出");
    }


    IEnumerator loadScene()
    {
        while (index < trans.Length)
        {
            MeshFilter mesh = trans[index].GetComponent<MeshFilter>();
            if (mesh != null)
            {
                //trans[index].gameObject.AddComponent<SingleObject>().SetSingleObject(trans[index].gameObject);
            }
            processBar.value = index;
            double a = (double)index;
            double b = (double)trans.Length;
            provessText.text = ((float)((a / (a + b)) * 100f)).ToString() + "%";
            textlog.text = trans[index].name;
            index++;
        }
        provessText.text = "100 %";
        UIManager.Instance.PopPanel();
        yield return null;
    }

    // Update is called once per frame
    //void Update()
    //{
    //    if (index < trans.Length)
    //    {
    //        MeshFilter mesh = trans[index].GetComponent<MeshFilter>();
    //        if (mesh != null)
    //        {
    //            trans[index].gameObject.AddComponent<SingleObject>().SetSingleObject(trans[index].gameObject);
    //        }
    //        nowprocess = (float)(index / trans.Length);
    //        processBar.value = index;
    //        double a = (double)index;
    //        double b = (double)trans.Length;
    //        provessText.text = ((int)((a / (a + b)) * 100f)).ToString() + "%";
    //        textlog.text = trans[index].name;
    //    }
    //    else
    //    {
    //        provessText.text = "100 %";
    //        mianCamer.position = new Vector3(canvasTransform.position.x, canvasTransform.position.y+ 50, canvasTransform.position.z);
    //        mianCamer.LookAt(canvasTransform);
    //        UIManager.Instance.PopPanel();
    //    }
    //    index++;
    //}
}
